package ${package.Controller};

import lombok.extern.slf4j.Slf4j;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import ${package.Service}.${table.serviceName};
import ${package.Entity}.${entity};
import ${util.getVoPackage(package.Entity)}.${entity}Vo;
<#if restControllerStyle>
import org.springframework.web.bind.annotation.RestController;
<#else>
import org.springframework.stereotype.Controller;
</#if>
<#if superControllerClassPackage??>
import ${superControllerClassPackage};
</#if>

/**
 * <p>
 * ${table.comment!} 前端控制器
 * </p>
 *
 * @author ${author}
 * @since ${date}
 */
@Slf4j
<#if restControllerStyle>
@RestController
<#else>
@Controller
</#if>
@RequestMapping("/${util.lowerFirst(table.controllerName)}")
<#if kotlin>
class ${table.controllerName}<#if superControllerClass??> : ${superControllerClass}()</#if>
<#else>
<#if superControllerClass??>
public class ${table.controllerName} extends ${superControllerClass} {
<#else>
public class ${table.controllerName} {
</#if>

    @Autowired
    private ${table.serviceName} ${util.lowerFirst(table.serviceName)};
    <#if (util.getPrimaryKeys(table.fields)?size ==1)>
    /**
    * 新增${table.comment}
    * @param entity
    * ${table.name}新增数据
    * @return 新增数据id
    */
    @ApiOperation(value = "${table.comment}", notes = "新增${table.comment}")
    @RequestMapping(value = "/add${entity}", method = RequestMethod.POST)
    <#assign primaryField=util.getPrimaryKeys(table.fields)[0]/>
    public ${primaryField.propertyType} add${entity}(@RequestBody ${entity} entity) {
        return ${util.lowerFirst(table.serviceName)}.add${entity}(entity);
    }
    </#if>

    /**
    * 修改${table.comment}
    * @param entity
    * ${table.name}修改数据
    * @return 修改记录数
    */
    @ApiOperation(value = "${table.comment}", notes = "修改${table.comment}")
    @RequestMapping(value = "/update${entity}", method = RequestMethod.POST)
    public int update${entity}(@RequestBody ${entity} entity) {
        return ${util.lowerFirst(table.serviceName)}.update${entity}(entity);
    }
    <#if (util.getPrimaryKeys(table.fields)?size ==1)>

    /**
    * 取指定ID的${table.comment}数据
    * @param id
    * ${table.name} 主键ID
    * @return 返回实体对象
    * 返回指定id ${table.comment}数据
    */
    @ApiOperation(value = "${table.comment}", notes = "取${table.comment}")
    @RequestMapping(value = "/get${entity}ById", method = RequestMethod.GET)
    <#assign primaryField=util.getPrimaryKeys(table.fields)[0]/>
    public ${entity}Vo get${entity}ById(@RequestParam ${primaryField.propertyType} id) {
        return ${util.lowerFirst(table.serviceName)}.get${entity}ById(id);
    }
    </#if>

    /**
    * ${table.comment}分页取数
    * @param entityVo
    * ${table.name} 分页取数参数，pageIndex=页码；pageSize=每页记录数；
    * @return
    * 返回分页数据
    */
    @RequestMapping(value = "/get${entity}List", method = RequestMethod.POST)
    public IPage<${entity}Vo> get${entity}List(@RequestBody ${entity}Vo entityVo){
        IPage<${entity}Vo> page = ${util.lowerFirst(table.serviceName)}.get${entity}List(entityVo);
        return page;
    }
 <#if (util.getPrimaryKeys(table.fields)?size ==1)>

    /**
    * 删除指定ID的${table.comment}数据
    * @param id
    * ${table.name} 主键ID
    */
    @ApiOperation(value = "${table.comment}", notes = "删除${table.comment}")
    @RequestMapping(value = "/delete${entity}ById", method = RequestMethod.GET)
    <#assign primaryField=util.getPrimaryKeys(table.fields)[0]/>
    public void delete${entity}ById(@RequestParam ${primaryField.propertyType} id) {
         ${util.lowerFirst(table.serviceName)}.delete${entity}ById(id);
    }
 </#if>
<#if (util.getPrimaryKeys(table.fields)?size ==1)>

    /**
    * 批量删除指定ID的${table.comment}数据
    * @param ids
    * ${table.name} 主键ID
    */
    @ApiOperation(value = "${table.comment}", notes = "批量删除${table.comment}")
    @RequestMapping(value = "/batchDelete", method = RequestMethod.POST)
    <#assign primaryField=util.getPrimaryKeys(table.fields)[0]/>
    public void batchDelete(@RequestParam ${primaryField.propertyType}[] ids) {
        ${util.lowerFirst(table.serviceName)}.batchDelete(ids);
    }
</#if>

}
</#if>
